import VJoyPlugin from '../../modules/phaser3-vjoy-plugin';

const _loadRocker = Symbol('_loadRocker');
const _firstCreated = Symbol('_firstCreated');
const _lastCreated = Symbol('_lastCreated');
const _RockerControlTarget = Symbol('_RockerControlTarget');
const _error = Symbol('_error');
export default class UtilsCreateRocker {
    constructor(_options, _that) {
        this.options = _options;
        this[_loadRocker](_that);
        if (!_options) return this[_error]('no options?');
        if (this.options.type == 'first') return this[_firstCreated](_that);
        if (this.options.type == 'last') return this[_lastCreated](_that);
        if (this.options.type == 'control') return this[_RockerControlTarget](_that);
    }

    [_loadRocker](_that) {
        _that.plugins.installScenePlugin('VJoyPlugin', VJoyPlugin, 'vjoy', _that);
    }

    [_firstCreated](_that) {
        return _that.add.joystick({
            sprites: {
                base: this.options.top || 'none',
                body: this.options.middle || 'none',
                cap: this.options.bottomImage || 'none'
            },
            singleDirection: false,
            maxDistanceInPixels: 100,
            device: 1 // 0 for mouse pointer (computer), 1 for touch pointer (mobile)
        });
    }

    [_lastCreated](_that) {
        _that[this.options.bindKey].on('pointerdown', _that[this.options.sceneKey].create, _that[this.options.sceneKey]);
        _that[this.options.bindKey].on('pointerup', _that[this.options.sceneKey].remove, _that[this.options.sceneKey]);

        _that.add.image(_that[this.options.sceneKey].settings, 'singleDirection');
        _that.add.image(_that[this.options.sceneKey].settings, 'maxDistanceInPixels');
        _that.add.image(_that[this.options.sceneKey].settings, 'device', {
            Computer: 0,
            Mobile: 1
        });
    }

    [_RockerControlTarget](_that) {
        _that[this.options.bindKey].body.velocity.set(this.options.cursors.deltaX * this.options.speed, this.options.cursors.deltaY * this.options.speed);
    }

    [_error](_err) {
        throw _err;
    }
    
}